Description
A cross-platform binding for performing packet capturing with node.js.
Requirements
Install
npm install cap
Examples
- Capture and decode all outgoing TCP data packets destined for port 80 on the interface for 192.168.0.10:
var Cap = require('cap').Cap;
var decoders = require('cap').decoders;
var PROTOCOL = decoders.PROTOCOL;
var c = new Cap();
var device = Cap.findDevice('192.168.0.10');
var filter = 'tcp and dst port 80';
var bufSize = 10 * 1024 * 1024;
var buffer = Buffer.alloc(65535);
var linkType = c.open(device, filter, bufSize, buffer);
c.setMinBytes && c.setMinBytes(0);
c.on('packet', function(nbytes, trunc) {
console.log('packet: length ' + nbytes + ' bytes, truncated? '
+ (trunc ? 'yes' : 'no'));
if (linkType === 'ETHERNET') {
var ret = decoders.Ethernet(buffer);
if (ret.info.type === PROTOCOL.ETHERNET.IPV4) {
console.log('Decoding IPv4 ...');
ret = decoders.IPV4(buffer, ret.offset);
console.log('from: ' + ret.info.srcaddr + ' to ' + ret.info.dstaddr);
if (ret.info.protocol === PROTOCOL.IP.TCP) {
var datalen = ret.info.totallen - ret.hdrlen;
console.log('Decoding TCP ...');
ret = decoders.TCP(buffer, ret.offset);
console.log(' from port: ' + ret.info.srcport + ' to port: ' + ret.info.dstport);
datalen -= ret.hdrlen;
console.log(buffer.toString('binary', ret.offset, ret.offset + datalen));
} else if (ret.info.protocol === PROTOCOL.IP.UDP) {
console.log('Decoding UDP ...');
ret = decoders.UDP(buffer, ret.offset);
console.log(' from port: ' + ret.info.srcport + ' to port: ' + ret.info.dstport);
console.log(buffer.toString('binary', ret.offset, ret.offset + ret.info.length));
} else
console.log('Unsupported IPv4 protocol: ' + PROTOCOL.IP[ret.info.protocol]);
} else
console.log('Unsupported Ethertype: ' + PROTOCOL.ETHERNET[ret.info.type]);
}
});
- Send an arbitrary packet: An arp request for example
var Cap = require('cap').Cap;
var c = new Cap();
var device = Cap.findDevice('192.168.1.200');
var filter = 'arp';
var bufSize = 10 * 1024 * 1024;
var buffer = Buffer.alloc(65535);
var linkType = c.open(device, filter, bufSize, buffer);
var buffer = Buffer.from([
0xff, 0xff, 0xff, 0xff, 0xff,0xff,
0x84, 0x8F, 0x69, 0xB7, 0x3D, 0x92,
0x08, 0x06,
0x00, 0x01,
0x08, 0x00,
0x06, 0x04,
0x00, 0x01,
0x84, 0x8f, 0x69, 0xb7, 0x3d, 0x92,
0xc0, 0xa8, 0x01, 0xc8,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xc0, 0xa8, 0x01, 0xc9
]);
try {
c.send(buffer, buffer.length);
} catch (e) {
console.log("Error sending packet:", e);
}
- List all network devices:
var Cap = require('cap').Cap;
console.dir(Cap.deviceList());
API
Cap events
- packet(< integer >nbytes, < boolean >truncated) - A packet
nbytes
in size was captured. truncated
indicates if the entire packet did not fit inside the Buffer supplied to open().
Cap methods
-
(constructor)() - Creates and returns a new Cap instance.
-
open(< string >device, < string >filter, < integer >bufSize, < Buffer >buffer) - (void) - Opens device
and starts capturing packets using filter
. To see the syntax for filter
check pcap-filter
man page. bufSize
is the size of the internal buffer that libpcap uses to temporarily store packets until they are emitted. buffer
is a Buffer large enough to store one packet. If open() is called again without a previous call to close(), an implicit close() will occur first.
-
close() - (void) - Stops capturing.
-
setMinBytes(< integer >nBytes) - (void) - (Windows ONLY) This sets the minimum number of packet bytes that must be captured before the full packet data is made available. If this value is set too high, you may not receive any packets until WinPCap's internal buffer fills up. Therefore it's generally best to pass in 0 to this function after calling open(), despite it resulting in more syscalls.
-
send(< Buffer >buffer[, < integer >nBytes]) - (void) - Sends an arbitrary, raw packet on the opened device. nBytes
is the number of bytes in buffer
to send (starting from position 0) and defaults to buffer.length
.
Cap static methods
-
findDevice([< string >ip]) - mixed - If ip
is given, the (first) device name associated with ip
, or undefined is returned if not found. If ip
is not given, the device name of the first non-loopback device is returned.
-
deviceList() - array - Returns a list of available devices and related information.
Decoders static methods
The following methods are available off of require('cap').decoders
. They parse the relevant protocol header and return an object containing the parsed information: